package com.giantlizard.cloud.base.utils;

import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;

public class ConversionUtil {

    public static <D, E> E convertToEntity(D dto, Class<E> entityClass) {
        E entity = BeanUtils.instantiateClass(entityClass);
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }

    public static <E, D> D convertToDto(E entity, Class<D> dtoClass) {
        D dto = BeanUtils.instantiateClass(dtoClass);
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    public static <D, E> List<E> convertToEntityList(List<D> dtoList, Class<E> entityClass) {
        return dtoList.stream()
                .map(dto -> convertToEntity(dto, entityClass))
                .collect(Collectors.toList());
    }

    public static <E, D> List<D> convertToDtoList(List<E> entityList, Class<D> dtoClass) {
        return entityList.stream()
                .map(entity -> convertToDto(entity, dtoClass))
                .collect(Collectors.toList());
    }

    public static <V, E> E convertToEntityFromVo(V vo, Class<E> entityClass) {
        E entity = BeanUtils.instantiateClass(entityClass);
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public static <E, V> V convertToVo(E entity, Class<V> voClass) {
        V vo = BeanUtils.instantiateClass(voClass);
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    public static <V, E> List<E> convertToEntityListFromVo(List<V> voList, Class<E> entityClass) {
        return voList.stream()
                .map(vo -> convertToEntityFromVo(vo, entityClass))
                .collect(Collectors.toList());
    }

    public static <E, V> List<V> convertToVoList(List<E> entityList, Class<V> voClass) {
        return entityList.stream()
                .map(entity -> convertToVo(entity, voClass))
                .collect(Collectors.toList());
    }
}
